﻿using System;
using AnjLab.SyncIT.Core.Data.Outlook;
using NUnit.Framework;

namespace AnjLab.SyncIT.Core.Tests.Data
{
    [TestFixture]
    public abstract class OutlookFoldersTestBase
    {
        public abstract OutlookFolder Folder { get; }

        [Test]
        public void TestFind()
        {
            var task10 = NewItem("scada #10: ticket10");
            task10.Save();
            var task11 = NewItem("scada #11: ticket11");
            task11.Save();
            var task12 = NewItem("scada #12: ticket12");
            task12.Save();
            var task13 = NewItem("scada #9: with next order");
            task13.Save();

            try
            {
                var task = Folder.FindItem("scada", "11");
                Assert.IsNotNull(task);
                Assert.AreEqual("scada #11: ticket11", task.Subject);

                task = Folder.FindItem("scada", "9");
                Assert.IsNotNull(task);
                Assert.AreEqual("scada #9: with next order", task.Subject);

            }
            finally
            {
                task10.Delete();
                task11.Delete();
                task12.Delete();
                task13.Delete();
            }
        }

        private IOutlookItem NewItem(string subject)
        {
            var item = Folder.CreateNewItem();
            item.Subject = subject;
            return item;
        }

        [Test]
        public void TestFindOpened()
        {
            var t1 = NewItem("cat1 #1: ticket 1");
            var t2 = NewItem("cat1 #2: ticket 2");
            var t10 = NewItem("cat1 #10: ticket 10");
            var t11 = NewItem("cat1 #11: ticket 11");
            var ta1 = NewItem("cat0 #1: ticket1000");
            var tb1 = NewItem("cat2 #1: ticket1000");
            try
            {
                t1.Save();
                t10.Save();
                t2.Complete = true;
                t2.Save();
                t11.Complete = true;
                t11.Save();
                ta1.Save();
                tb1.Save();

                var tasks = Folder.FindOpenedItems("cat1");
                Assert.IsNotNull(tasks);
                Assert.AreEqual(2, tasks.Count);

                Assert.AreEqual(t1.Subject, tasks[0].Subject);
                Assert.AreEqual(t10.Subject, tasks[1].Subject);
            }
            finally
            {
                t1.Delete();
                t2.Delete();
                t10.Delete();
                t11.Delete();
                ta1.Delete();
                tb1.Delete();
            }
        }

        [Test]
        public void TestFindModifiedTasks()
        {
            var t1 = NewItem("cat1 #1: ticket 1");
            var t2 = NewItem("cat1 #2: ticket 2");
            try
            {
                Console.WriteLine(t1.LastModificationTime);
                t1.Save();
                Console.WriteLine(t1.LastModificationTime);
                t2.Save();
                Console.WriteLine(t2.LastModificationTime);
                var tasks = Folder.FindModifiedItems(DateTime.Now, "cat1");
                Assert.AreEqual(0, tasks.Count);
                t1.Body = "nice";
                t1.Save();

                Console.WriteLine(DateTime.Now.AddMinutes(-2));
                tasks = Folder.FindModifiedItems(DateTime.Now.AddMinutes(-2), "cat1");
                Assert.AreEqual(2, tasks.Count);
            }
            finally
            {
                t1.Delete();
                t2.Delete();
            }
        }
    }
}
